const app = getApp();
var cloudDB = require("../../utils/CloudDB.js");
var httpClient = require("../../utils/HttpClient.js");
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    ColorList: app.globalData.ColorList,
    userName: '',
    password: '',
    logining: ''
  },
  changeInput(e) {
    let changed = {};
    let prop = e.currentTarget.dataset.prop
    changed[prop] = e.detail.value;
    this.setData(changed)
  },
  onLoad: function() {
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        cloudDB.selectRow("user", {
          _openid: res.result.openid
        }, function(res) {
          if (res.data.length > 0) {
            app.globalData.userInfo = res.data[0];
            wx.redirectTo({
              url: '/pages/index/index',
            })
          }
        })
      },

    })
  },

  onGetUserInfo: function(e) {
    if (this.data.userName == '' || this.data.password == '') {
      wx.showToast({
        icon: 'none',
        title: "用户名和密码都不能为空"
      })
    } else {
      this.setData({
        logining: 'logining'
      });
      httpClient.superAjax({
        isWebService: true,
        url: 'F0006Service.asmx/ChangedLatestLoginMin',
        data: {
          username: this.data.userName,
          pwd: this.data.password,
          _this: this
        },
        success: function(data) {
          if (!data.Code && data.length > 0) { //登录成功
            var user = {
              wx: e.detail.userInfo,
              mp: data[0]
            };
            cloudDB.insertRow('user', user, function() {
              app.globalData.userInfo = user;
              wx.redirectTo({
                url: '../index/index',
              })
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: data.Message
            })
          }
        },
        complete: function() {
          this.data._this.setData({
            logining: ''
          });
        }
      })
    }
  }
});